package com.fudan.adse.controller;

import com.fudan.adse.api.CommonPage;
import com.fudan.adse.api.CommonResult;
import com.fudan.adse.domain.po.FilmType;
import com.fudan.adse.domain.vo.RowPieceWithDetailVO;
import com.fudan.adse.service.FilmTypeService;
import com.fudan.adse.service.RowPieceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 查看排片数据
 *
 * @author Jerry Zhang
 * create: 2022-11-23 09:49
 */
@RestController
@RequestMapping("/api/v1/rms/outer")
public class RowPieceOuterController {
    @Autowired
    private RowPieceService rowPieceService;
    @Autowired
    private FilmTypeService filmTypeService;

    /**
     * 获取某场次座位售出情况
     *
     * @param rowPieceId
     * @return
     */
    @GetMapping(value = "/row-piece/seats")
    public CommonResult<RowPieceWithDetailVO> getRowPieceWithSeats(@RequestParam("rowPieceId") Integer rowPieceId) {
        return CommonResult.success(rowPieceService.getRowPieceWithSeats(rowPieceId));
    }

    /**
     * 获取某影片排片信息，包括影院
     *
     * @param filmId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/row-piece/cinemas")
    public CommonResult<CommonPage<RowPieceWithDetailVO>> getCinemasByFilmId(@NotBlank @RequestParam("filmId") Integer filmId,
                                                                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                             @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(rowPieceService.getRowPiecesByFilmId(filmId, pageNum, pageSize)));
    }


    /**
     * 获取指定影院的排片情况，包括影片
     *
     * @param cinemaId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/row-piece/films")
    public CommonResult<CommonPage<RowPieceWithDetailVO>> getFilmsByCinemaId(@NotBlank @RequestParam("cinemaId") Integer cinemaId,
                                                                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                             @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(rowPieceService.getRowPiecesByCinemaId(cinemaId, pageNum, pageSize)));
    }

    /**
     * 获取某影片、某类型影片、某状态影片排片信息
     *
     * @param title
     * @param types
     * @param statuses
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/row-piece/filterBy")
    public CommonResult<CommonPage<RowPieceWithDetailVO>> filterFilmsBy(@RequestParam(value = "title", required = false) String title,
                                                                        @RequestParam(value = "types", required = false) String types,
                                                                        @RequestParam(value = "statuses", required = false) String statuses,
                                                                        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                        @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        List<String> typeList = new ArrayList<>();
        if (types != null && !types.isEmpty()) {
            typeList = new ArrayList<>(Arrays.asList(types.split(",")));
        }
        List<String> statusList = new ArrayList<>();
        if (statuses != null && !statuses.isEmpty()) {
            statusList = new ArrayList<>(Arrays.asList(statuses.split(",")));
        }
        return CommonResult.success(CommonPage.restPage(rowPieceService.filterByConditions(title, typeList, statusList, pageNum, pageSize)));
    }

    /**
     * 获取所有电影分类
     *
     * @return
     */
    @GetMapping("/types")
    public CommonResult<List<FilmType>> getAllTypes() {
        return CommonResult.success(filmTypeService.getAllTypes());
    }

}
